package com.hnust.dao;

import com.hnust.domain.Teacher;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 老师表操作的DAO层
 * @author 小游
 * @date 2020/10/29
 */
@Mapper
@Repository
public interface TeacherDao {

    /**
     *  根据老师id获取老师信息
     * @param id 老师id
     * @return 返回老师结构体
     */
    @Select("select tea_number from teacher where tea_id = #{id}")
    @ResultType(Teacher.class)
    String getTeacherIdById(String id);

    /**
     *  根据老师教工号获取老师信息
     * @param tea_number 老师教工号
     * @return 返回老师结构体
     */
    @Select("select * from teacher where tea_number = #{tea_number}")
    @ResultType(Teacher.class)
    Teacher getTeacherById(String tea_number);

    @Select("SELECT name FROM `college` WHERE col_id=#{id};")
    @ResultType(String.class)
    String getColName(String id);

    //个人信息中更新学院信息
    @Update("update teacher set col_id=#{collegeId} where tea_id=#{teacherId}")
    @ResultType(Integer.class)
    Integer updateCollege(String collegeId,String teacherId);

    //通过老师id查询老师信息
    @Select("select * from teacher where tea_id = #{teacherId}")
    @ResultType(Teacher.class)
    Teacher getTeacher(String teacherId);

    //更新个人信息
    @Update("update teacher set name=#{name},pho_number=#{phone},e_mail=#{email}," +
            "tea_number=#{number},password=#{password},col_id=#{collegeId}" +
            "where tea_id=#{teacherId}")
    @ResultType(Integer.class)
    Integer updateInfo(String name,String phone,String email,String teacherId,String number,
                       String collegeId,String password);

    /**
     * 通过id，password寻找这个老师是否在数据库里，用于登录判断
     * @param id // 老师id
     * @param password 密码
     * @return 返回个数
     */
    @Select("select count(*) from teacher where tea_id = #{id} and password = #{password}")
    int findTeacherByIdPass(String id, String password);

    @Select("select count(*) from teacher where tea_number = #{id} and password = #{password}")
    int findTeacherByNumberPass(String id, String password);
    /*
     * @Author Tonited
     * @Description //TODO 根据教师id获取教师名
     * @Date 14:48 2020/10/31
     * @Param [teaId]
     * @return java.lang.String
     **/
    @Select("select name from teacher where tea_id = #{teaId}")
    @ResultType(String.class)
    String getTeacherNameById(String teaId);
//通过姓名查老师信息
    @Select("select * from teacher where name like concat('%',#{teacherName},'%')")
    @ResultType(Teacher.class)
    List<Teacher> getTeacherByName(String teacherName);

    @InsertProvider(type = TeacherDao.insertTeacher.class, method = "perform")
    @ResultType(Boolean.class)
    Boolean insertTeacher(String id, String name, String number, String password, String phone, String email,String collegeId);
    class insertTeacher{
        public String perform(String id, String name, String number, String password, String phone, String email,String collegeId){
            String sql = "insert into teacher (tea_id, tea_number, name, password, col_id ,pho_number, e_mail) values(#{id}, #{number}, #{name}, #{password}, #{collegeId},";
            if(phone == null){ // course_id是空的，所以是查找所有课程的
                sql += "'未知', ";
            }else{
                sql += "#{phone}, ";
            }
            if(email==null){
                sql += "'未知'";
            }else{
                sql += "#{email}";
            }
            sql+=")";
            return sql;
        }
    }

    @Select("select * from teacher where pho_number = #{phone}")
    @ResultType(Teacher.class)
    Teacher getTeacherByPhone(String phone);
}
